<template>
  <div class="excel">
    <c-button type="link" @click="importTable">导入Excel</c-button>
    <input
      type="file"
      style="display: none;"
      ref="importBtn"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
      @change="importFile"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      resData: []
    };
  },
  watch: {
    resData() {
      this.$emit("change", this.resData);
    }
  },
  methods: {
    importTable() {
      // 先清空原来的数据，确保触发change
      this.$refs.importBtn.value = "";
      this.$refs.importBtn.click();
    },
    importFile() {
      let file = this.$refs.importBtn.files;
      if (!file) {
        // this.fullscreenLoading = false
        return;
      }
      var f = file[0];
      var reader = new FileReader();
      let $t = this;
      reader.onload = function(e) {
        var data = e.target.result;
        if ($t.rABS) {
          $t.wb = window.XLSX.read(btoa(this.fixdata(data)), {
            // 手动转化
            type: "base64"
          });
        } else {
          $t.wb = window.XLSX.read(data, {
            type: "binary"
          });
        }
        let json = window.XLSX.utils.sheet_to_json(
          $t.wb.Sheets[$t.wb.SheetNames[0]]
        );
        // $t.dealFile($t.analyzeData(j)); // analyzeData: 解析导入数据
        $t.dealFile(json);
      };
      if (f) {
        if ($t.rABS) {
          reader.readAsArrayBuffer(f);
        } else {
          reader.readAsBinaryString(f);
        }
      }
    },
    dealFile: function(data) {
      // 处理导入的数据
      this.$refs.importBtn.files.value = null;
      if (data.length <= 0) {
        // this.errorDialog = true
        // this.errorMsg = '请导入正确信息'
      } else {
        const keyList = [
          ["品牌", "brand"],
          ["规格", "spec"],
          ["材质", "material"],
          ["其他", "other"],
          ["仓库简称", "warehouseName"],
          ["数量", "amount"]
        ];
        data.forEach(item => {
          keyList.forEach(i => {
            if (item[i[0]] !== undefined && item[i[0]] !== null) {
              item[i[1]] = item[i[0]] + "";
              delete item[i[0]];
            }
          });
          item.premium = item.升贴水;
          delete item.升贴水;
          item.price = item.价格;
          delete item.价格;
        });
        data.forEach((item, index) => {
          let noneList = [];
          keyList.forEach(i => {
            if (
              item[i[1]] !== undefined &&
              item[i[1]].toString() &&
              item[i[1]].trim() !== ""
            ) {
              noneList.push(item[i[1]]);
            }
          });
          if (noneList.length == 0) data.splice(index, 1);
        });
        this.resData = data;
      }
    },
    fixdata: function(data) {
      // 文件流转BinaryString
      var o = "";
      var l = 0;
      var w = 10240;
      for (; l < data.byteLength / w; ++l) {
        o += String.fromCharCode.apply(
          null,
          new Uint8Array(data.slice(l * w, l * w + w))
        );
      }
      o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
      return o;
    }
  }
};
</script>
